package com.yinxun.controller;

import com.yinxun.dao.phoenix.PHRInfoDao;
import com.yinxun.dao.phoenix.UserInfoDao;
import com.yinxun.dto.AdviceDTO;
import com.yinxun.dto.AdvicePicsDTO;
import com.yinxun.dto.MedicalRecordDto;
import com.yinxun.dto.MedicinesDTO;
import com.yinxun.dto.PHRDto;
import com.yinxun.dto.RecordPicsDTO;
import com.yinxun.entity.UserInfo;
import com.yinxun.util.BeanMapperUtil;
import com.yinxun.vo.MedicalRecordVO;
import com.yinxun.vo.QueryVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.lang.reflect.InvocationTargetException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

/**
 * phoenix测试控制器
 */
@Slf4j
@Api(value = "PHR-HBase", tags = "诊疗记录接口")
@RequestMapping("phoenix")
@RestController
public class MedicalHBaseController {

	@Autowired
	PHRInfoDao phrInfoMapper;

	@Resource
	private PageHelper phoenixPageHelper;

	@ApiOperation(value = "保存诊疗记录")
	@PostMapping("save")
	public MedicalRecordDto getAll(@RequestBody MedicalRecordVO vo)
			throws IllegalAccessException, InvocationTargetException {
		// 数据处理
		log.info("保存诊疗记录入参:{}", JSON.toJSONString(vo));
		PHRDto phrDto = new PHRDto();
		// vo 转dto
		phrDto = BeanMapperUtil.map(vo, PHRDto.class);
		phrDto.setMedicines(JSON.toJSONString(vo.getPrescriptionVO().getMedicineVOs()));
		phrDto.setRecordPics(JSON.toJSONString(vo.getPrescriptionVO().getRecordPicVOs()));
		phrDto.setDescription(vo.getAdviceVO().getDescription());
		BeanUtils.copyProperties(phrDto, vo.getPrescriptionVO());
		BeanMapperUtil.map(vo.getAdviceVO(), phrDto.getClass());
		System.out.println(JSON.toJSONString(phrDto));
		// rowkey
		String resultString = MD5Encoder.encode(DigestUtils.md5(phrDto.getAccountNo() + phrDto.getMemberId()));
		String prefix = resultString.substring(0, 5);
		phrDto.setAccount_memberId(prefix);
		log.info("保存诊疗记录出参:{}", JSON.toJSONString(phrDto));
		// hbase 读取
		phrInfoMapper.addPhr(phrDto);
		return null;
	}

	@ApiOperation(value = "查询诊疗记录")
	@PostMapping("query")
	public MedicalRecordDto getAll(@RequestBody QueryVO vo) {
		// 数据处理
		return null;
	}

}
